<template>
  <div>
    <!-- 轮播图 -->
    <van-swipe :autoplay="3000" class="van_swipe">
      <van-swipe-item v-for="(item, index) in banners" :key="index">
        <img class="van_img" :src="item.imageUrl" />
      </van-swipe-item>
    </van-swipe>
    <!-- 推荐歌单 -->
    <div class="m-homeremd">
      <h2 class="remd_ti">推荐歌单</h2>
    </div>
    <!-- 话语速歌 -->
    <div class="remd_ul">
      <div
        class="remd_li"
        @click="add(item)"
        v-for="(item, index) in result"
        :key="index"
      >
        <div class="remd_img">
          <img :src="item.picUrl" alt="" />
          <div class="box">
            <van-icon class=" icon_a" name="service-o" />
            <span class="remd_num"> {{ item.playCount | get_num }} </span>
          </div>
        </div>
        <p class="remd_text">{{ item.name }}</p>
      </div>
    </div>
    <!-- 推荐歌单 -->
    <div class="m-homeremd">
      <h2 class="remd_ti">最新音乐</h2>
    </div>
    <!-- 将id_list（集合） 与歌曲index（下标）存至vuex 方便后续上下首切换 -->
    <song
      v-for="(item, index) in newsong"
      :key="index"
      :id_list="id_list"
      :ids="index"
      :song_data="{
        song_name: item.song.name,
        song_ar: item.song.artists,
        song_people: item.song.album.name,
        id: item.id,
        duration: item.duration,
      }"
    >
    </song>
    <!-- 尾端 -->
    <!-- <ul class="remd_newsg">
      <li class="remd_song " v-for="(item, index) in newsong" :key="index"> -->
    <!-- 左边 -->
    <!-- <div class="remd_left">
          <div class="f_thide">
            {{ item.song.name }}
          </div>
          <div class="sginfo">
            {{ item.song.artists[0].name }} - {{ item.song.name }}
          </div>
        </div> -->
    <!-- 右边 -->
    <!-- <div class="remd_right" @click="playMusic(item)">
          <svg class="icon icon_a" aria-hidden="true">
            <use xlink:href="#iconbofang"></use>
          </svg>
        </div>
      </li>
    </ul> -->

    <!-- 渲染二级路由 -->

    <router-view />
  </div>
</template>

<script>
// 引入导入
import { get_banner, get_personalized, get_newsong } from "../../api/recommend";
import song from "../../components/song";
export default {
  data() {
    return {
      banners: [],
      result: [],
      newsong: [],
      musicUrl: "",
    };
  },
  components: {
    song,
  },
  methods: {
    add(e) {
      this.$router.push({
        path: "/songlist",
        query: {
          id: e.id,
        },
      });
    },

    // playMusic(e) {
    //   this.$store.state.songId = e.id;
    //   this.$store.state.songUrl = e.picUrl;
    // },
  },
  filters: {
    get_num(num) {
      let new_num = num / 10000;
      if (new_num > 10000) {
        new_num = new_num / 10000;
        return new_num.toFixed(1) + "亿";
      }
      return new_num.toFixed(1) + "万";
    },
  },
  created() {
    // 轮播图
    get_banner().then((res) => {
      // console.log(res);
      this.banners = res.data.banners;
    });
    //推荐歌单
    get_personalized().then((res) => {
      // console.log(res);
      this.result = res.data.result;
    });
    // 最新歌单
    get_newsong().then((res) => {
      console.log(res.data);
      this.newsong = res.data.result;
    });
  },
  computed: {
    // 使用计算属性筛选出新歌榜所以id的集合
    id_list() {
      return this.newsong.map((el) => el.id);
    },
  },
};
</script>

<style lang="scss" scoped>
.van_swipe {
  .van_img {
    width: 100%;
    height: 180px;
  }
}

.m-homeremd {
  padding-top: 20px;
  .remd_ti {
    position: relative;
    padding-left: 9px;

    font-size: 17px;
    height: 20px;
    line-height: 20px;
    display: flex;
  }
  .remd_ti:after {
    content: "";
    position: absolute;
    left: 0;
    top: 50%;
    margin-top: -9px;
    width: 2px;
    height: 16px;
    background-color: #d33a31;
  }
}

.remd_ul {
  display: flex;
  justify-content: space-between;
  flex-flow: wrap;
  .remd_li {
    width: 33.33%;
    margin-bottom: 16px;
    .remd_img:after {
      content: " ";
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 20px;
      z-index: 2;
      background-image: linear-gradient(
        180deg,
        rgba(0, 0, 0, 0.2),
        rgba(0, 0, 0, 0)
      );
    }
    .remd_img {
      position: relative;
      img {
        width: 100%;
        padding-left: 0;
        padding-right: 2px;
      }
      .box {
        position: absolute;
        right: 1px;
        top: 2px;
        z-index: 3;
        color: #fff;
        .icon_a {
          font-size: 12px;
          top: 2px;
        }
        .remd_num {
          font-size: 11px;
        }
      }
    }
    .remd_text {
      overflow: hidden;
      padding: 6px 2px 0 6px;
      min-height: 30px;
      line-height: 1.2;
      font-size: 12px;
      text-align: left;
      // white-space: wrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
</style>
